﻿using System.Collections.Generic;
using Fiddler;
using FiddlerFirePHPInspector.BusinessObjects.Wildfire;

namespace FiddlerFirePHPInspector.Processors
{
    public static class WildfireProcessor
    {
        private static WildfireData GetOrCreateWildfireData(int pluginNumber, Dictionary<int, WildfireData> wildfirePlugins)
        {
            WildfireData wildfireData;
            if (wildfirePlugins.ContainsKey(pluginNumber))
            {
                wildfireData = wildfirePlugins[pluginNumber];
            }
            else
            {
                wildfireData = new WildfireData { Index = pluginNumber };
                wildfirePlugins.Add(pluginNumber, wildfireData);
            }
            return wildfireData;
        }

        private static void ProcessWildfireHeader(HTTPHeaderItem httpHeaderItem, Dictionary<int, WildfireData> result)
        {
            string[] strings = httpHeaderItem.Name.Split('-');

            if (strings[2].Equals("Protocol"))
            {
                int pluginNumber;
                if (int.TryParse(strings[3], out pluginNumber))
                {
                    WildfireData wildfireData = GetOrCreateWildfireData(pluginNumber, result);
                    wildfireData.Protocol = httpHeaderItem.Value;
                }
            }
            else
            {
                int pluginNumber;
                if (int.TryParse(strings[2], out pluginNumber))
                {
                    WildfireData wildfireData = GetOrCreateWildfireData(pluginNumber, result);

                    int majorSequenceNumber;
                    if (int.TryParse(strings[3], out majorSequenceNumber))
                    {
                        if (majorSequenceNumber == 1)
                        {
                            int minorSequenceNumber;
                            if (int.TryParse(strings[4], out minorSequenceNumber))
                            {
                                if (majorSequenceNumber == 1)
                                {
                                    int logItemNumber;
                                    if (int.TryParse(strings[5], out logItemNumber))
                                    {
                                        WildfirePayload wildfirePayload = new WildfirePayload()
                                                                              {
                                                                                  SequenceNumber = logItemNumber,
                                                                                  Payload = httpHeaderItem.Value
                                                                              };

                                        wildfireData.Payloads.Add(wildfirePayload);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        switch (strings[3])
                        {
                            case "Plugin":
                                wildfireData.Plugin = httpHeaderItem.Value;
                                break;

                            case "Structure":
                                wildfireData.Structure = httpHeaderItem.Value;
                                break;
                        }
                    }
                }
            }
        }

        public static List<WildfireData> ExtractWildfireData(HTTPResponseHeaders httpResponseHeaders)
        {
            Dictionary<int, WildfireData> wildfireDataDictionary = new Dictionary<int, WildfireData>();

            for (var x = 0; x < httpResponseHeaders.Count(); x++)
            {
                HTTPHeaderItem httpHeaderItem = httpResponseHeaders[x];

                if (httpHeaderItem.Name.StartsWith("X-Wf-"))
                {
                    ProcessWildfireHeader(httpHeaderItem, wildfireDataDictionary);
                }
            }

            List<WildfireData> wildfireDatas = new List<WildfireData>();
            foreach (KeyValuePair<int, WildfireData> keyValuePair in wildfireDataDictionary)
            {
                wildfireDatas.Add(keyValuePair.Value);
            }

            return wildfireDatas;
        }
    }
}
